package com.qing.demo.netty.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;

/**
 * @author caozhipeng
 * @date 2025/7/25
 *
 * netty通信就向一个流水channel管道，我们可以在管道的中间插入一些‘挡板’为我们服务。
 * 比如字符串的编码解码
 * ChannelInitializer:挡板
 */
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
//        String 编码解码
        socketChannel.pipeline().addLast(new StringDecoder(Charset.forName("utf-8")));
        socketChannel.pipeline().addLast(new StringEncoder(Charset.forName("utf-8")));
//        自定义的编码器
//        消息出站处理器，在Client发送消息时候会触发此处理器
        socketChannel.pipeline().addLast(new MyOutMsgHandler());
//        消息入站处理器
        socketChannel.pipeline().addLast(new MyInMsgHandler());
    }
}
